
package ro.imc.rest.client.display.components;

import java.util.List;

import ro.imc.rest.shared.dto.LearningObject;
import ro.imc.rest.shared.dto.LearningObjectType;
import ro.imc.rest.shared.dto.RestLogin;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Label;

public class CoursesFrom extends Composite {

	private static CatalogsFromUiBinder uiBinder =
		GWT.create(CatalogsFromUiBinder.class);
	@UiField
	AbsolutePanel courseDiplay;
	@UiField
	AbsolutePanel courseInfo;
	@UiField
	Label strUser;
	@UiField
	AbsolutePanel coursePanel;

	interface CatalogsFromUiBinder extends UiBinder<Widget, CoursesFrom> {
	}
	private RestLogin user;

	public CoursesFrom(RestLogin user, List<LearningObject> coursesList) {

		initWidget(uiBinder.createAndBindUi(this));
		this.user = user;
		for (LearningObject obj : coursesList) {
			LearningObjectFrom form = new LearningObjectFrom(obj);
			if (obj.getType() == LearningObjectType.COURSE) {
				form.sinkEvents(Event.ONCLICK);
				form.addHandler(
					new LearningObjectClickHandlers(obj), ClickEvent.getType());
			}
			coursePanel.add(form);
		}
	}

	private class LearningObjectClickHandlers implements ClickHandler {

		private LearningObject obj;

		public LearningObjectClickHandlers(LearningObject obj) {

			this.obj = obj;
		}

		@Override
		public void onClick(ClickEvent event) {
			courseInfo.clear();
			courseInfo.add(new CourseInfoForm(user, obj));
		}

	}

}
